/*
 * @Description:
 * @Version: 2.0
 * @Autor: zss
 * @Date: 2025-01-08 16:22:38
 * @LastEditors: zss
 * @LastEditTime: 2025-03-04 13:21:35
 */
import { T_task } from '@/obj/class';
import { ifs, Tsrv } from '@/services';
import { N_RectifierSettings } from './type';

class T_cmd {
  // 主组件箱
  private compBox: ifs.I_compBox;

  // 主组件ID
  private compID: string;

  // 主组件实例
  private self: N_RectifierSettings.cRectifierSettings;

  private task: T_task = new T_task(5000);

  // 当前轮询是否开启
  public isOpenTask: boolean = false;

  // 主组件created里初始化
  public init(
    self: N_RectifierSettings.cRectifierSettings,
    compBoxInfo: ifs.I_compBoxInfo
  ) {
    this.self = self;
    this.compBox = compBoxInfo.TcompBox;
    this.compID = compBoxInfo.compID;
  }

  public startTask() {
    this.task.addTask(this.readData);
    this.task.startTask();
    this.isOpenTask = true;
  }

  public stopTask() {
    this.task.delTask(this.readData);
    this.task.stopTask();
    this.isOpenTask = false;
  }

  private readData = () => {
    Tsrv.readPlcData(this.self.ecDetCodes).then(
      (data: ifs.I_scadaReadResp['data'] | boolean) => {
        if (typeof data !== 'boolean') {
          this.self.updateData(data);
        }
      }
    );
  };

  public getTcmd(): T_cmd {
    return this;
  }
}

interface I_cmd extends T_cmd {}

export { I_cmd, T_cmd };
